$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        currentTaskId: 0,
        taskList: null,
        openTid: 0,
        ifExpand: 0,
        init: function () {
            $('.xh-datagrid-toolbar').parseToolbar();
            this.taskList = $('#taskList');
            $('#startTime').datebox({showSeconds: false});
            $('#endTime').datebox({showSeconds: false});
            $('#editTask').setButtonDisable('eu');
            $('#delTask').setButtonDisable('eu');
            $('#linkTask').setButtonDisable('eu');
            this.funObj().initTree();
            $('#changeStatus').menubutton({
                menu: '#alterstatus'
            });
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('.ifExpand').click(function () {
                var className = $(this).children('span').attr('class');
                $this.funObj().expandRelateTask(className);
            });
            $('#editTask').click(function () {
                $this.funObj().editTask();
            });
            $('#delTask').click(function () {
                $this.funObj().delTask();
            });
            $('#linkTask').click(function () {
                $this.funObj().linkTask();
            });
            $('#bt_list_search').click(function () {
                $this.funObj().searchTask();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                initTree: function () {
                    $this.taskList.treegrid({
                        url: moduleUrl + 'taskstats/getTask',
                        method: 'post',
                        rownumbers: true,
                        pagination: true,
                        pageSize: 50,
                        pageList: [20, 50, 100, 200],
                        idField: 'iTaskId',
                        treeField: 'sTaskName',
                        singleSelect: true,
                        onSelect: function (row) {
                            $this.currentTaskId = row['iTaskId'];
                            $this.openTid = row['iTaskId'];
                            $this.taskList.treegrid('expand', $this.currentTaskId);
                            $('#editTask').setButtonEnable('eu');
                            $('#delTask').setButtonEnable('eu');
                            $('#linkTask').setButtonEnable('eu');
                        },
                        onBeforeSelect: function (row) {
                            if ($this.ifExpand > 0) {
                                if ($this.ifExpand == 1) {
                                    var datas = row.expandContent;
                                    if (datas.length > 0) {
                                        var content = '<tr class="expandId' + row.iTaskId + '"><td colspan="5" style="padding-left: 30px;border:0px;"><table style="border-left: 1px dotted lightgrey"><tr><th height="20" style="text-align: center;background-color: #00c898;color: #FFFFFF;" width="200">任务名称</th><th height="20" style="text-align: center;background-color: #00c898;color: #FFFFFF;" width="80" >任务状态</th><th height="20" style="text-align:center;background-color:#00c898;color: #FFFFFF;" width="100">执行人</th></tr>';
                                        var content1 = '<tr class="expandId' + row.iTaskId + '"><td colspan="11"><table><tr><th height="20" style="text-align:center;"> </th></tr>';
                                        $.each(datas, function (index, data) {
                                            content += '<tr><td height="20" style="text-align: center">' + data.sTaskName + '</td><td height="20" style="text-align: center">' + taskStatus[data.iTaskStatus] + '</td><td height="20" style="text-align: center">' + data.sStaffName + '</td></tr>';
                                            content1 += '<tr><td height="20" style="text-align: center"> </td></tr>';
                                        });
                                        content += '</table></td></tr>';
                                        content1 += '</table></td></tr>';
                                    }
                                    $("tr[node-id='" + row.iTaskId + "']").eq(0).after(content);
                                    $("tr[node-id='" + row.iTaskId + "']").eq(1).after(content1);
                                } else if ($this.ifExpand == 2) {
                                    $('.expandId' + row.iTaskId).remove();
                                }
                                $this.ifExpand = 0;
                                return false;
                            }
                            if ($this.currentTaskId == 0) {
                                return true;
                            } else {
                                if ($this.currentTaskId == row['iTaskId']) {
                                    $('#editTask').setButtonDisable('eu');
                                    $('#delTask').setButtonDisable('eu');
                                    $('#linkTask').setButtonDisable('eu');
                                    $this.taskList.treegrid('unselect', $this.currentTaskId);
                                    $this.taskList.treegrid('collapse', $this.currentTaskId);
                                    $this.currentTaskId = 0;
                                    $this.openTid = 0;
                                    return false;
                                }
                            }
                        },
                        onBeforeCheck: function (row) {
                            if ($this.ifExpand > 0) {
                                if ($this.ifExpand == 1) {
                                    var datas = row.expandContent;
                                    if (datas.length > 0) {
                                        var content = '<tr class="expandId' + row.iTaskId + '"><td colspan="5" style="padding-left: 30px"><table style="border-left: 1px dotted lightgrey"><tr><th height="20" style="text-align: center;background-color: #0079c8;border-width: 1px; 1px 1px 1px;color: #FFFFFF;border-color: #FFFFFF" width="250">任务名称</th><th height="20" style="text-align: center;background-color: #0079c8;border-width: 1px; 1px 1px 1px;color: #FFFFFF;" width="80" >任务状态</th></tr>';
                                        var content1 = '<tr class="expandId' + row.iTaskId + '"><td colspan="3"><table><tr><th height="20" style="text-align:center;background-color:#0079c8;border-width: 1px; 1px 1px 1px;color: #FFFFFF;" width="100">执行人</th></tr>';
                                        $.each(datas, function (index, data) {
                                            content += '<tr><td height="20" style="text-align: center">' + data.sTaskName + '</td><td height="20" style="text-align: center">' + taskStatus[data.iTaskStatus] + '</td></tr>';
                                            content1 += '<tr><td height="20" style="text-align: center">' + data.sStaffName + '</td></tr>';
                                        });
                                        content += '</table></td></tr>';
                                        content1 += '</table></td></tr>';
                                    }
                                    $("tr[node-id='" + row.iTaskId + "']").eq(0).after(content);
                                    $("tr[node-id='" + row.iTaskId + "']").eq(1).after(content1);
                                } else if ($this.ifExpand == 2) {
                                    $('.expandId' + row.iTaskId).remove();
                                }
                                $this.ifExpand = 0;
                                return false;
                            }
                            if ($this.currentTaskId == 0) {
                                return true;
                            } else {
                                if ($this.currentTaskId == row['iTaskId']) {
                                    $this.taskList.treegrid('unselect', $this.currentTaskId);
                                    $this.taskList.treegrid('uncheckRow', $this.currentTaskId);
                                    $this.currentTaskId = 0;
                                    $this.openTid = 0;
                                    $('#editTask').setButtonDisable('eu');
                                    $('#delTask').setButtonDisable('eu');
                                    $('#linkTask').setButtonDisable('eu');
                                    return false;
                                }
                            }
                        },
                        onLoadSuccess: function (row, data) {
                            function getParent(pid) {
                                var taskObj = $this.taskList.treegrid('find', pid);
                                if (taskObj) {
                                    $this.taskList.treegrid('expand', pid);
                                    if (taskObj['iParentId'] != 0) {
                                        $this.taskList.treegrid('expand', taskObj['iParentId']);
                                        var parentObj = $this.taskList.treegrid('find', taskObj['iParentId']);
                                        if (parentObj) {
                                            if (parentObj['iParentId'] != 0) {
                                                getParent(parentObj['iParentId']);
                                            }
                                        }
                                    }
                                }
                            }

                            if ($this.openTid) {
                                getParent($this.openTid);
                                $this.taskList.treegrid('select', $this.openTid);
                            }
                            $('.ifExpand').click(function () {
                                var className = $(this).children('span').attr('class');
                                if (className == 'datagrid-row-expander datagrid-row-expand') {
                                    $this.ifExpand = 1;
                                    $(this).children('span').attr('class', 'datagrid-row-expander datagrid-row-collapse');
                                } else if (className == 'datagrid-row-expander datagrid-row-collapse') {
                                    $this.ifExpand = 2;
                                    $(this).children('span').attr('class', 'datagrid-row-expander datagrid-row-expand');
                                }
                            });
                        },
                        onBeforeLoad: function (row, param) {
                            $this.taskList.treegrid('clearSelections');
                            $this.currentTaskId = 0;
                        },
                        onBeforeExpand: function (row) {
                            $('.expandId' + row.iTaskId).prev().find('span.datagrid-row-expander').attr('class', 'datagrid-row-expander datagrid-row-expand');
                            $('.expandId' + row.iTaskId).remove();
                        },
                        onBeforeCollapse: function (row) {
                            $('.expandId' + row.iTaskId).prev().find('span.datagrid-row-expander').attr('class', 'datagrid-row-expander datagrid-row-expand');
                            $('.expandId' + row.iTaskId).remove();
                        }

                    })
                },
                changeStatus: function (status) {
                    // 开启或者完成任务
                    //item 的相关属性参见API中的menu
                    var taskObj = $this.taskList.treegrid('getSelections')[0];
                    var pid = taskObj['iParentId'];
                    if (pid) {
                        taskObj['_parentId'] = pid;
                        var pTaskObj = $this.taskList.treegrid('getParent', $this.currentTaskId);
                        if (pTaskObj) {
                            if (pTaskObj['iTaskStatus'] == 3 || pTaskObj['iTaskStatus'] == 4) {
                                window.top.xhTop.showError('错误', '父级任务已完成或已关闭');
                                return false;
                            }
                        }
                    }
                    if ($this.taskList.treegrid('getSelections').length == 0) {
                        window.top.xhTop.showError('错误', '请选择一个任务');
                        return false;
                    }
                    var iTaskId = taskObj['iTaskId'];
                    if (status == 3) {
                        window.top.swal({
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            title: '实际工时',
                            width: 400,
                            input: 'text',
                            preConfirm: function (dRealTaskTime) {
                                return new Promise(function (resolve, reject) {
                                    if (dRealTaskTime) {
                                        var reg1 = /^\+?(\d*\.\d{1,2})$/;
                                        var reg2 = /^\+?[1-9][0-9]*$/;
                                        if (!reg1.test(dRealTaskTime) && !reg2.test(dRealTaskTime)) {
                                            reject('实际工时格式错误');
                                            return false;
                                        }
                                        ;
                                    } else {
                                        reject('实际工时不能为空');
                                        return false;
                                    }
                                    $.ajax({
                                        url: moduleUrl + 'task/changeStatus',
                                        data: {iTaskId: iTaskId, iTaskStatus: status, dRealTaskTime: dRealTaskTime},
                                        type: 'POST',
                                        success: function (data) {
                                            window.top.xhTop.showSuccess(
                                                data.msg
                                            );
                                            $this.taskList.treegrid('reload');
                                        },
                                        error: function (data) {
                                            window.top.xhTop.showError(
                                                data.responseJSON.error,
                                                data.responseJSON.msg
                                            );
                                        }
                                    });
                                });
                            }
                        });
                    } else {
                        $.ajax({
                            url: moduleUrl + 'task/changeStatus',
                            data: {iTaskId: iTaskId, iTaskStatus: status},
                            type: 'POST',
                            success: function (data) {
                                window.top.xhTop.showSuccess(
                                    data.msg
                                );
                                $this.taskList.treegrid('reload');
                                projectList.treegrid('reload');
                            },
                            error: function (data) {
                                window.top.xhTop.showError(
                                    data.responseJSON.error,
                                    data.responseJSON.msg
                                );
                            }
                        });
                    }
                },
                editTask:function () {
                    var obj = $this.taskList.treegrid('getSelections')[0];
                     // if (eleid == 'bt_list_edit') {
                        if (!obj) {
                            return false;
                        } else {
                            if (obj['iTaskStatus'] == 3 || obj['iTaskStatus'] == 4) {
                                window.top.xhTop.showError('错误', '任务已完成或已关闭');
                                return false;
                            }
                        }
                        var title = '编辑任务';
                        var id = $this.taskList.treegrid('getSelections')[0]['iTaskId'];
                        window.top.xhTop.showModal({
                            title: title,
                            content: '<iframe src="' + moduleUrl + 'task/taskForm?iTaskId=' + id  + '&ifTaskStats=1'+ '"  style="width: 100%;border-style: none;padding: 0px;height: 400px"  scrolling="yes"   ></iframe>',
                            button: '提交',
                            backClose: false,
                        });
                    // } else if (eleid == 'bt_list_del') {
                    //     var obj =  $this.taskList.treegrid('getSelections')[0];
                    //     if (!obj) return false;
                    //     myWindow.initPopUp({title: '确认删除', text: '确定要删除该任务吗?'}, function () {
                    //         $.ajax({
                    //             url: moduleUrl + 'task/delTaskById',
                    //             data: {iTaskId: obj['iTaskId']},
                    //             type: 'POST',
                    //             success: function (data) {
                    //                 window.top.xhTop.showSuccess(
                    //                     data.msg
                    //                 );
                    //                 $this.taskList.treegrid('clearSelections');
                    //                 $this.taskList.treegrid('reload');
                    //             },
                    //             error: function (data) {
                    //                 window.top.xhTop.showError(
                    //                     data.responseJSON.error,
                    //                     data.responseJSON.msg,
                    //                 )
                    //             }
                    //         })
                    //     });
                    // } else if (eleid == 'bt_list_link') {
                    //     var obj = taskList.treegrid('getSelections')[0];
                    //     if (!obj) return false;
                    //     window.top.xhTop.showModal({
                    //         title: '关联任务',
                    //         width: 660,
                    //         content: '<iframe src="' + moduleUrl + 'task/publicTasks?iTaskId=' + obj['iTaskId'] + '"  style="width: 100%;border-style: none;padding: 0px;height: 400px"  scrolling="yes"   ></iframe>',
                    //         backClose: false
                    //     });
                    // }
                },
                delTask:function () {
                    var obj =  $this.taskList.treegrid('getSelections')[0];
                    if (!obj) return false;
                    myWindow.initPopUp({title: '确认删除', text: '确定要删除该任务吗?'}, function () {
                        $.ajax({
                            url: moduleUrl + 'task/delTaskById',
                            data: {iTaskId: obj['iTaskId']},
                            type: 'POST',
                            success: function (data) {
                                window.top.xhTop.showSuccess(
                                    data.msg
                                );
                                $this.taskList.treegrid('clearSelections');
                                $this.taskList.treegrid('reload');
                            },
                            error: function (data) {
                                window.top.xhTop.showError(
                                    data.responseJSON.error,
                                    data.responseJSON.msg
                                )
                            }
                        })
                    });
                },
                linkTask:function () {
                    var obj = $this.taskList.treegrid('getSelections')[0];
                        if (!obj) return false;
                        window.top.xhTop.showModal({
                            title: '关联任务',
                            width: 660,
                            content: '<iframe src="' + moduleUrl + 'task/publicTasks?iTaskId=' + obj['iTaskId'] +'&ifTaskStats=1'+ '"  style="width: 100%;border-style: none;padding: 0px;height: 400px"  scrolling="yes"   ></iframe>',
                            backClose: false
                        });

                },
                searchTask:function () {
                        var status = $('#taskStatus').val();
                        var level = $('#taskLevel').val();
                        var userid = $('#memberid').val();
                        var startTime = $('#startTime').datebox('getValue');
                        var endTime = $('#endTime').datebox('getValue');
                        $this.taskList.treegrid('load', {
                            status: status,
                            level: level,
                            userid: userid,
                            start:startTime,
                            end:endTime,
                        });

                }
            }
        }
    }
};

